package com.netease.spring.autoproxy;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class PerformanceThresholdInterceptor implements MethodInterceptor {

	private final long thresholdInMillis;
	
	
	public PerformanceThresholdInterceptor(long thresholdInMillis) {
		this.thresholdInMillis = thresholdInMillis;
	}

	@Override
	public Object invoke(MethodInvocation invocation) throws Throwable {
		long t = System.currentTimeMillis();
		Object o = invocation.proceed();
		t = System.currentTimeMillis() - t;
		if(t > thresholdInMillis) {
			warnThresholdExceeded();
		} else {
			NoWarnThresholdExceeded();
		}
		return o;
	}
	
	private void warnThresholdExceeded() {
		System.out.println("Danger! Danger!");
	}

	private void NoWarnThresholdExceeded() {
		System.out.println("No Danger...");
	}
}
